package mysql

import "gorm.io/gorm"

type House struct {
	gorm.Model
	UserId        int64  `gorm:"type:int(11);comment:用户id"`
	HouseName     string `gorm:"type:varchar(20);comment:房源名称"`
	HouseFloor    string `gorm:"type:varchar(10);comment:房源楼层"`
	HouseType     string `gorm:"type:varchar(30);comment:房源类型"`
	Address       string `gorm:"type:varchar(50);comment:房源地址"`
	HouseCover    string `gorm:"type:varchar(200);comment:房源图片"`
	Area          int64  `gorm:"type:int(11);comment:房源面积"`
	Price         int64  `gorm:"type:int(11);comment:房源价格"`
	HouseDescribe string `gorm:"type:varchar(100);comment:房源介绍"`
	HouseStatus   int64  `gorm:"type:tinyint(1);default:1;comment:房源状态:1:待出售,2:上架,3:下架"`
}

func (h *House) Create() error {
	return Db.Create(&h).Error
}
func (h *House) HouseView(houseId int64) error {
	return Db.Where("id = ?", houseId).Limit(1).Find(&h).Error
}

func (h *House) HouseUpdateStatus(houseId, houseStatus int64) error {
	return Db.Model(&House{}).Where("id = ?", houseId).Update("house_status", houseStatus).Error
}

// starPriceendPrice
func (h *House) HouseList(saraPrice, entPrice int64) (HouseList []House, err error) {
	db := Db.Debug()
	if saraPrice > 0 {
		db = db.Where("price => ?", saraPrice)
	}
	if entPrice > 0 {
		db = db.Where("price <= ?", entPrice)
	}
	err = Db.Order("created_at desc").Limit(1).Find(&HouseList).Error
	return
}
